package database.dao.Impl;

import database.dao.CustomerDao;
import entity.Customer;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName CustomerDaoImpl
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/17 8:53
 */
public class CustomerDaoImpl extends BaseDao implements CustomerDao {

    @Override
    public Integer addCustomer(Customer customer) {
        String psql="insert into Customer(name,password,telephone,address,sex)" +
                "values(?,?,?,?,?)";
        Object[] values={customer.getName(),customer.getPassword(),customer.getTelephone(),customer.getAddress(),customer.getSex()};
        super.executeUpdate(psql,values);
        return getAutoId("Customer","Customer_id");
    }

    @Override
    public Customer selectCustomer(String customer_id) {
        String psql="SELECT * FROM customer WHERE customer_id= ? ";
        Object[] value={ customer_id };
        ResultSet resultSet= super.executeQuerySQL(psql,value);

        Customer customer=new Customer();
        try {
            if(resultSet.next()){
                customer.setCustomer_id(resultSet.getString("customer_id"));
                customer.setName(resultSet.getString("name"));
                customer.setTelephone(resultSet.getString("telephone"));
                customer.setAddress(resultSet.getString("address"));
                customer.setSex(resultSet.getString("Sex"));
                customer.setPassword(resultSet.getString("Password"));
            }else{
                System.out.println("未查询到");
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return customer;
    }

    @Override
    public void updateCustomer(Customer customer) {
        String psql="update Customer name set name=?,password =?,telephone =?,address =?,sex=? where customer_id=?";
        Object[] values={customer.getName(),customer.getPassword(),customer.getTelephone(),customer.getAddress(),customer.getSex(),customer.getCustomer_id()};
        super.executeUpdate(psql,values);
    }

}
